import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class ImagePanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private BufferedImage sourceImage;
    private BufferedImage destImage;
    private BufferedImage saveImage;

    public ImagePanel() {

    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
        if (sourceImage != null) {
            g2d.drawImage(sourceImage, 0, 0, sourceImage.getWidth(),
                    sourceImage.getHeight(), null);
            if (destImage != null) {
                g2d.drawImage(destImage, sourceImage.getWidth() + 10, 0,
                        destImage.getWidth(), destImage.getHeight(), null);
            }
        }
    }

    public void grayFilter() {
        ColorToneFilter filter = new ColorToneFilter();
        //SepiaToneFilter filter = new SepiaToneFilter();
        destImage = filter.filter(sourceImage, null);
        saveImage = destImage;
    }

    public void binaryFilter() {
        grayFilter();
        BinaryImageFilter filter = new BinaryImageFilter();
        destImage = filter.filter(destImage, null);
        saveImage = destImage;
    }

    public void gammaFilter() {
        grayFilter();
        GammaImageFilter filter = new GammaImageFilter();
        destImage = filter.filter(destImage, null);
        saveImage = destImage;
    }

    public void meanFilter() {
        MeanFilter filter = new MeanFilter();
        destImage = filter.filter(sourceImage, null);
        saveImage = destImage;
    }

    public void medianFilter() {
        MedianFilter filter = new MedianFilter();
        destImage = filter.filter(sourceImage, null);
        saveImage = destImage;
    }

    public void sharpenFilter() {
        SharpenFilter filter = new SharpenFilter();
        destImage = filter.filter(sourceImage, null);
        saveImage = destImage;
    }

    public void histogramFilter() {
        HistogramFilter filter = new HistogramFilter();
        destImage = filter.filter(sourceImage, null);
        saveImage = destImage;
    }

    public void sobelFilter() {
        SobelPrewittFilter filter = new SobelPrewittFilter();
        filter.setType(SobelPrewittFilter.SOBOL_TYPE);
        filter.setDirection(SobelPrewittFilter.Y_DIRECTION);
        destImage = filter.filter(sourceImage, null);
        saveImage = destImage;
    }

    public void prewittFilter() {
        SobelPrewittFilter filter = new SobelPrewittFilter();
        filter.setDirection(SobelPrewittFilter.PREWITT_TYPE);
        filter.setDirection(SobelPrewittFilter.X_DIRECTION);
        destImage = filter.filter(sourceImage, null);
        saveImage = destImage;
    }
    public BufferedImage processOut() {

        //ColorToneFilter filter = new ColorToneFilter();
        //BufferedImage res = filter.filter(sourceImage, null);
        BufferedImage res = saveImage;
        return res;
    }
    public BufferedImage getSourceImage() {
        return sourceImage;
    }

    public void setSourceImage(BufferedImage sourceImage) {
        this.sourceImage = sourceImage;
    }
    public BufferedImage getDestImage() {
        return destImage;
    }

    public void setDestImage(BufferedImage destImage) {
        this.destImage = destImage;
    }

    public static String getRandomString(int length){
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; i++){
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public void saveImageFile(BufferedImage bi) throws IOException {
        String fileName = getRandomString(6) + ".jpg";
        File outputfile = new File(fileName);
        ImageIO.write(bi, "jpg", outputfile);
    }
}
